<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				postUserCacheKey:"hr-train-trainPlanAdjust-postUser-cache",
				projectCacheKey:"hr-train-trainPlanAdjust-project-cache",
				userCacheKey1:"hr-train-trainPlanAdjust-user-cache2",
				userCacheKey2:"hr-train-trainPlanAdjust-user-cache3",
				userCacheKey3:"hr-train-trainPlanAdjust-user-cache4",
				relevantAbilityCacheKey:"hr-train-trainPlanAdjust-relevantAbility-cache",
				partakeUserCache:"hr-train-trainPlanAdjust-partakeUser-cache",
				initValue:{
					reviewerStr:[],
					reviewerStr1:[],
					reviewerStr2:[]
				}
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId','orgId'])
		},
		methods:{
			formSubmit(){
				let _that = this
				_that.formdata.orgId = _that.orgId
				if(_that.formdata.userId1 && _that.formdata.userName1){
					_that.formdata.reviewerStr.push({
						userName:_that.formdata.userName1,
						postName:_that.formdata.postName1,
						userId:_that.formdata.userId1,
						postId:_that.formdata.postId1
					})
					_that.formdata.reviewerStr = JSON.stringify(_that.formdata.reviewerStr)
				}else{
					_that.formdata.reviewerStr = "[]"
				}
				if(_that.formdata.userId2 && _that.formdata.userName2){
					_that.formdata.reviewerStr1.push({
						userName:_that.formdata.userName2,
						postName:_that.formdata.postName2,
						userId:_that.formdata.userId2,
						postId:_that.formdata.postId2
					})
					_that.formdata.reviewerStr1 = JSON.stringify(_that.formdata.reviewerStr1)
				}else{
					_that.formdata.reviewerStr1 = "[]"
				}
				if(_that.formdata.userId3 && _that.formdata.userName3){
					_that.formdata.reviewerStr2.push({
						userName:_that.formdata.userName3,
						postName:_that.formdata.postName3,
						userId:_that.formdata.userId3,
						postId:_that.formdata.postId3
					})
					_that.formdata.reviewerStr2 = JSON.stringify(_that.formdata.reviewerStr2)
				}else{
					_that.formdata.reviewerStr2 = "[]"
				}
				_that.om.formSave("trainPlanAdjust/doAdd",_that.formdata,_that)
			},
			getPostList(id,title,postId,postName){
				let _that = this 
				_that.om.request(
					"orgInstPosUserOfficial/getPostList",
					{
						"user.id":id
					}
				).then(res=>{
					if(res.length){
						_that.$refs.autoform.changeEditor(title,postId,"data",configSelectData(res,"id","posname",true))
						_that.$refs.autoform.formdata[postId]=res[0].id
						_that.$refs.autoform.formdata[postName]=res[0].posname
					}else{
						_that.om.toast("该人员在系统中无任职岗位",3000)
					}
				})
			},
			intLoad(options){
				let _that = this
				let ajaxArr = [
					urlUtil.zposUrl(options.code),
					urlUtil.sysdictComboboxUrl("38404")
				]
				formModelLoad(ajaxArr,function(res,mainData){
				changeEditor("topinfo","zposId","data",configSelectData(res[0],"id","posname"),mainData)
				changeEditor("title1","trainTypeId","data",configSelectData(res[1],"id","dicname"),mainData)	
				changeEditor("title1", "trainApplyShowId", {
					link: "../../public/pageSearch/pageSearch",
					"linkParam": {
						other: JSON.stringify({}),
						namefield: "projectName",
						extrafield: "trainObj",
						notefield: "trainTypeName",
						searchfield: "q",
						initload: true,
						url: "trainNotice/likeSerchShow",
						cachename: _that.projectCacheKey
					}
				},null,mainData)
				changeEditor('title2', 'userId1', {
					link:"../../public/pageSearch/pageSearch",
					"linkParam": {
						other: JSON.stringify({
						}),
						namefield:urlUtil.userPageUrl().namefield,
						searchfield:"q",
						initload:true,
						url:urlUtil.userPageUrl().url,
						cachename:_that.userCacheKey1,
						placeholder:"请输入姓名"
					}
				},null,mainData)
				changeEditor('title3', 'userId2', {
					link:"../../public/pageSearch/pageSearch",
					"linkParam": {
						other: JSON.stringify({
						}),
						namefield:urlUtil.userPageUrl().namefield,
						searchfield:"q",
						initload:true,
						url:urlUtil.userPageUrl().url,
						cachename:_that.userCacheKey2,
						placeholder:"请输入姓名"
					}
				},null,mainData)
				changeEditor('title4', 'userId3', {
					link:"../../public/pageSearch/pageSearch",
					"linkParam": {
						other: JSON.stringify({
						}),
						namefield:urlUtil.userPageUrl().namefield,
						searchfield:"q",
						initload:true,
						url:urlUtil.userPageUrl().url,
						cachename:_that.userCacheKey3,
						placeholder:"请输入姓名"
					}
				},null,mainData)
				changeEditor('title1', 'userPostId', {
					link:"../../public/pageSearch/pageSearch",
					"linkParam": {
						other: JSON.stringify({
						}),
						namefield:urlUtil.userPageUrl().namefield,
						searchfield:"q",
						url:"orgInstPosUserOfficial/findListByOrg",
						cachename:_that.postUserCacheKey,
						placeholder:"请输入姓名"
					}
				},null,mainData)
				changeEditor('title1', 'relevantAbilityIdStr', {
					"linkParam": {
						other: JSON.stringify({
							code: '35905'
						}),
						namefield: 'text',
						likefield: 'text',
						initload: true,
						url: '/tsysdict/findLastChildTree',
						cachename: _that.relevantAbilityCacheKey
					}
				}, null, mainData);
				changeEditor('title1', 'partakeUserIdStr', {
					linkParam: {
						namefield: urlUtil.userPageUrl().namefield,
						searchfield: "q",
						initload:true,
						url: urlUtil.userPageUrl().url,
						cachename: _that.partakeUserCache,
						placeholder: urlUtil.userPageUrl().prompt,
						pagination: true
					}
				  },null,mainData)
				},options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.intLoad(options)
		},
		onShow() {
				let _that = this;
				if (_that.$refs.autoform) {
					uni.getStorage({
						key:_that.projectCacheKey,
						success:(res)=>{
							if(res.data){
								let d = res.data
								let formdata= _that.$refs.autoform.formdata
								formdata.trainApplyShowId = d.id
								formdata.projectName = d.projectName
								formdata.trainTypeId = d.trainTypeId
								formdata.trainTypeName = d.trainTypeName
								_that.$refs.autoform.initSelectIndex("title1","trainTypeId")
								formdata.startDate = d.startDate
								formdata.endDate = d.endDate
								formdata.timeNum = d.timeNum
								formdata.partakeUserNum = d.partakeUserNum
								formdata.userPostId = d.userPostId
								formdata.userPostName = d.userPostName
								formdata.relevantAbilityIdStr = d.relevantAbilityIdStr
								formdata.relevantAbilityNameStr = d.relevantAbilityNameStr
								formdata.content = d.content
								_that.om.request(
									"tsysdict/getComboTree", {
										code: "35905"
									}
								).then(res => {
									let newArr = []
									if(d.relevantAbilityIdStr != null){
										let relevantAbilityArr = d.relevantAbilityIdStr.split(",")
										for(let obj of res){
											if(relevantAbilityArr.indexOf(obj.id+"") > -1){
												newArr.push(obj)
											}
										}
										_that.$refs.autoform.changeEditor('title1', 'relevantAbilityIdStr', {
											initSelect: newArr
										})
									}
								})
								uni.removeStorage({
									key:_that.projectCacheKey
								})
							}
						}
					})
					uni.getStorage({
						key:_that.userCacheKey1,
						success:(res)=>{
							if(res.data){
								let d = res.data
								let formdata = _that.$refs.autoform.formdata
								formdata.userId1 = d.id 
								formdata.userName1 = d.username
								_that.getPostList(d.id,"title2","postId1","postName1")
								uni.removeStorage({
									key:_that.userCacheKey1
								})
							}
						}
					})
					uni.getStorage({
						key:_that.userCacheKey2,
						success:(res)=>{
							if(res.data){
								let d = res.data
								let formdata = _that.$refs.autoform.formdata
								formdata.userId2 = d.id 
								formdata.userName2 = d.username
								_that.getPostList(d.id,"title3","postId2",'postName2')
								uni.removeStorage({
									key:_that.userCacheKey2
								})
							}
						}
					})
					uni.getStorage({
						key:_that.userCacheKey3,
						success:(res)=>{
							if(res.data){
								let d = res.data
								let formdata = _that.$refs.autoform.formdata
								formdata.userId3 = d.id 
								formdata.userName3 = d.username
								_that.getPostList(d.id,"title4","postId3","postName3")
								uni.removeStorage({
									key:_that.userCacheKey3
								})
							}
						}
					})
					uni.getStorage({
						key:_that.relevantAbilityCacheKey,
						success:(res)=>{
							if(res.data){
								let formdata = _that.$refs.autoform.formdata
								let d = res.data
								formdata.relevantAbilityIdStr = d.map(e => e.id).join(",")
								formdata.relevantAbilityNameStr = d.map(e => e.text).join(",")
								_that.$refs.autoform.changeEditor('title1', 'relevantAbilityIdStr', {
									initSelect: d
								})
								uni.removeStorage({
									key:_that.relevantAbilityCacheKey
								})
							}
						}
					})
					uni.getStorage({
						key:_that.partakeUserCache,
						success:(res)=>{
							let formdata = _that.$refs.autoform.formdata
							let d = res.data
							formdata.partakeUserIdStr = d.map(e => e.id).join(",")
							formdata.partakeUserNameStr = d.map(e => e.username).join(",")
							_that.$refs.autoform.changeEditor('title1', 'partakeUserIdStr', {
								initSelect: d
							})
							uni.removeStorage({
								key:_that.partakeUserCache
							})
						}
					})
					uni.getStorage({
						key:_that.postUserCacheKey,
						success:(res)=>{
							if(res.data){
								let d = res.data
								let formdata = _that.$refs.autoform.formdata
								formdata.userPostId = d.id
								formdata.userPostName = d.userName
								uni.removeStorage({
									key:_that.postUserCacheKey
								})
							}
						}
					})
					
				}
		}
	}
</script>

<style>
</style>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
